package com.yl.ydk.api

import com.yl.ydk.base.Constant
import com.yl.ydk.bean.*
import com.yl.ydk.bean.body.GoodsDetailBody
import com.google.gson.Gson
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import rx.Observable


class Api(okHttpClient: OkHttpClient) {

    init {
        val retrofit: Retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(Gson()))
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .client(okHttpClient)
            .baseUrl(Constant.BASE_URL)
            .build()
        service =
            retrofit.create(ApiService::class.java)
    }

    companion object {
        private var instance: Api? = null
        private var service: ApiService? = null
        fun getInstance(): Api? {
            synchronized(Api::class.java) {
                if (instance == null) {
                    instance =
                        Okhttp.provideOkHttpClient()?.let { Api(it) }
                }
                return instance
            }
        }

        fun getMap(
            vararg s: String
        ): Map<String, String>? {
            val map: MutableMap<String, String> =
                HashMap()
            if (s.size % 2 != 0) {
                return null
            } else {
                var i = 0
                while (i < s.size) {
                    map[s[i]] = s[++i]
                    i++
                }
            }
            return map
        }
    }

    //登录接口
    fun getLogin(vararg s: String?): Observable<UserInfoBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getLogin(it) }
    }

    //发送验证码
    fun getSendSmsCode(vararg s: String?): Observable<SendSmsCodeBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getSendSmsCode(it) }
    }

    //单文件上传(base64)
    fun getBase64Upload(requestBody: MultipartBody?): Observable<Base64UploadBean>? {
        return requestBody?.let { service?.getBase64Upload(it) }
    }

    //设置用户资料
    fun getSaveUser(vararg s: String?): Observable<SaveUserBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getSaveUser(it) }
    }

    //视频分类
    fun getVideoCategories(vararg s: String?): Observable<VideoCategoriesBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getVideoCategories(it) }
    }

    //视频列表
    fun getVideoList(vararg s: String?): Observable<VideoListBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getVideoList(it) }
    }

    //获取疾病分类
    fun getHealthGDiseaseCate(vararg s: String?): Observable<DiseaseCateBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getHealthGDiseaseCate(it) }
    }

    //商品列表
    fun getGoodsList(vararg s: String?): Observable<GoodsListBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getGoodsList(it) }
    }

    //商品详情
    fun getGoodsDetail(boby: GoodsDetailBody): Observable<GoodsDetailBean>? {
        return boby.let { service?.getGoodsDetail(boby) }
    }

    //获取疾病详情
    fun getGetDiseaseDetail(vararg s: String?): Observable<DiseaseDetailBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getGetDiseaseDetail(it) }
    }

    //搜索疾病列表
    fun getGetDiseaseList(vararg s: String?): Observable<DiseaseSearchBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getGetDiseaseList(it) }
    }

    //文章列表
    fun getSiteAricle(vararg s: String?): Observable<SiteAricleBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getSiteAricle(it) }
    }

    //获取文章详情
    fun getSiteAricleDetail(vararg s: String?): Observable<SiteAricleDetailBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getSiteAricleDetail(it) }
    }

    //健康自测分类
    fun getHealthtestCategory(vararg s: String?): Observable<HealthtestCategoryBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getHealthtestCategory(it) }
    }

    //获取所有问卷
    fun getTestPapers( body: RequestBody?): Observable<TestPapersBean>? {
        return body?.let { service?.getTestPapers(body) }
    }

    //题目自测选项
    fun getHealthtestQuestion(body: RequestBody?): Observable<HealthtestQuestionBean>? {
        return body?.let { service?.getHealthtestQuestion(body) }
    }

    //提交测试
    fun getTestFinish(body: RequestBody?): Observable<TestFinishBean>? {
        return body?.let { service?.getTestFinish(body) }
    }

    //直播视频列表
    fun getLiveList(): Observable<LiveListBean>? {
        return service?.getLiveList()
    }

    //直播详情
    fun getLiveDetail(vararg s: String?): Observable<LiveDetailBean>? {
        return getMap(*s as Array<out String>)?.let { service?.getLiveDetail(it) }
    }

}